package com.zjhd.sealcutting.service.impl;

import com.google.common.collect.Maps;
import com.zjhd.sealcutting.dao.EntryInfoMapper;
import com.zjhd.sealcutting.dao.UserInfoMapper;
import com.zjhd.sealcutting.domain.CreativeTask;
import com.zjhd.sealcutting.domain.EntryInfo;
import com.zjhd.sealcutting.domain.UserInfo;
import com.zjhd.sealcutting.param.EntrySearchQuery;
import com.zjhd.sealcutting.service.EntryInfoService;
import com.zjhd.sealcutting.utils.response.ResponseResult;
import com.zjhd.sealcutting.utils.response.ResultGenerator;
import com.zjhd.sealcutting.vo.SearchResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * TODO 词条管理
 *
 * @author wangchao
 * @version 1.0
 * @date 2020/11/26 17:42
 */
@Service
public class EntryInfoServiceImpl implements EntryInfoService {

    @Autowired
    private EntryInfoMapper entryInfoMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;
    /**
     * TODO 分页查询词条
     * @author wangchao
     * @date 2020/11/26 17:50
     * @param
     * @return
     */
    @Override
    public SearchResult queryEntryParamlist(EntrySearchQuery param) throws Exception {
        //定义map 封装查询参数到map中
        Map<String, Object> map = Maps.newLinkedHashMap();
        if(param.getUserId()!=null){
            UserInfo userInfo=userInfoMapper.selectByPrimaryKey(param.getUserId());
            if(userInfo!=null && !userInfo.getUserGroup().equals("1")){
                map.put("userId",userInfo.getId());
            }
        }
        //当前页  分页数
        map.put("pageIndex", (Integer.parseInt(param.getPageIndex()) - 1) * Integer.parseInt(param.getPageSize()));
        map.put("pageSize", Integer.parseInt(param.getPageSize()));
        List<EntryInfo> entryInfos=entryInfoMapper.queryParamEntryList(map);
        long totalCount=entryInfoMapper.queryParamEntryListCount(map);
        if(entryInfos.size()>0){
            return new SearchResult.Builder<List<EntryInfo>>()
                    .setCurrent(Integer.parseInt(param.getPageIndex()))
                    .setPageSize(Integer.parseInt(param.getPageSize()))
                    .setTotalCount(totalCount)
                    .setPagedItems(entryInfos == null ? new ArrayList() : entryInfos)
                    .build();
        }else {
            return new SearchResult.Builder().setPagedItems(new ArrayList()).build();
        }
    }

    /**
     * TODO  根据用户查询词条
     * @author wangchao
     * @date 2020/12/14 11:02
     * @param
     * @return
     */
    @Override
    public List<EntryInfo> queryByUserNameEntry(Integer userId) throws Exception {
        List<EntryInfo> entryInfos=new ArrayList<>();
        UserInfo userInfo=userInfoMapper.selectByPrimaryKey(userId);
        if(userInfo!=null && userInfo.getUserGroup().equals("1")){
            //查询所有已分配用户词条
            entryInfos =entryInfoMapper.queryAllocatedUserEntry();
        }else {
            //根据用户查询用户所分配词条
            entryInfos =entryInfoMapper.queryByUserIdEntry(userId);
        }
        return entryInfos;
    }

    /**
     * TODO  新增词条
     * @author wangchao
     * @date 2020/12/16 9:45
     * @param
     * @return
     */
    @Override
    public ResponseResult insertEntry(EntryInfo entryInfo) throws Exception {
        try {
            int i=entryInfoMapper.insert(entryInfo);
            if(i>0){
                return ResultGenerator.genSuccessResult("新增词条成功!");
            }else {
                return ResultGenerator.genFailResult("新增词条失败!");
            }
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return ResultGenerator.genFailResult("新增词条失败!");
    }

    /**
     * TODO  修改词条
     * @author wangchao
     * @date 2020/12/16 9:45
     * @param
     * @return
     */
    @Override
    public ResponseResult updateEntry(EntryInfo entryInfo) throws Exception {
        try {
            int i=entryInfoMapper.updateByPrimaryKeySelective(entryInfo);
            if(i>0){
                return ResultGenerator.genSuccessResult("修改词条成功!");
            }else {
                return ResultGenerator.genFailResult("修改词条失败!");
            }
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return ResultGenerator.genFailResult("修改词条失败!");
    }

    /**
     * TODO 查询词条详情
     * @author wangchao
     * @date 2020/12/16 9:45
     * @param
     * @return
     */
    @Override
    public EntryInfo queryByIdEntry(Integer id) throws Exception {
        return entryInfoMapper.selectByPrimaryKey(id);
    }

    /**
     * TODO 删除词条
     * @author wangchao
     * @date 2020/12/16 9:45
     * @param
     * @return
     */
    @Override
    public boolean delEntry(Integer id) throws Exception {
        boolean bool=false;
        int i=entryInfoMapper.deleteByPrimaryKey(id);
        if(i>0){
            bool=true;
        }
        return bool;
    }
}
